package com.ilop.sthome.ui.fragment.message;

import com.example.common.base.BasePresentFragment;
import com.example.common.utils.LiveDataBus;
import com.example.common.view.refresh.CustomRefreshView;
import com.ilop.sthome.app.MyApplication;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.DeviceShareBean;
import com.ilop.sthome.mvp.contract.message.ShareContract;
import com.ilop.sthome.mvp.present.message.SharePresenter;
import com.ilop.sthome.ui.activity.device.GatewayMainActivity;
import com.ilop.sthome.ui.adapter.device.DeviceShareAdapter;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.FragmentDeviceShareBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2020-04-01.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备分享
 */
public class DeviceShareFragment extends BasePresentFragment<SharePresenter, FragmentDeviceShareBinding> implements ShareContract.IView{

    private DeviceShareAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_device_share;
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresenter = new SharePresenter(mContext);
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new DeviceShareAdapter(mContext);
        mDBind.deviceShareList.setAdapter(mAdapter);
        mDBind.deviceShareList.setLoadMoreEnable(true);
    }

    @Override
    protected void initData() {
        super.initData();
        LiveDataBus.get().with(CommonId.SHARE_CANCEL, String.class).observe(this, s -> {
            if (!MyApplication.getCurActivity().getClass().getName().equals(GatewayMainActivity.class.getName())){
                mPresenter.cancelShare(s);
            }
        });
        LiveDataBus.get().with(CommonId.SHARE_CONFIRM, String.class).observe(this, s -> mPresenter.confirmShare(s));
        LiveDataBus.get().with(CommonId.SHARE_DELETE, DeviceShareBean.class).observe(this, share -> {
            assert share != null;
            if (!MyApplication.getCurActivity().getClass().getName().equals(GatewayMainActivity.class.getName())){
                mPresenter.onConfirmUnbind(share.getTargetId(), share.getReceiverAlias());
            }
        });
        LiveDataBus.get().with(CommonId.SHARE_CLEAR, Integer.class).observe(this, integer -> mPresenter.clearShareNoticeList());
        LiveDataBus.get().with(CommonId.SHARE_REFRESH, Integer.class).observe(this, integer -> mPresenter.getShareNoticeList());
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.deviceShareList.setOnLoadListener(new CustomRefreshView.OnLoadListener() {
            @Override
            public void onRefresh() {
                showProgressDialog();
                mPresenter.getShareNoticeList();
                mDBind.deviceShareList.complete();
            }

            @Override
            public void onLoadMore() {
                mDBind.deviceShareList.onNoMore();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getShareNoticeList();
    }

    @Override
    public void showShareList(List<DeviceShareBean> shareList) {
        mAdapter.setList(shareList);
        mDBind.deviceShareList.onNoMore();
    }

    @Override
    public void showNoSharer() {
        mAdapter.setList(null);
        mDBind.deviceShareList.setEmptyView(getString(R.string.no_message), R.mipmap.sharelist_empty);
    }

    @Override
    public void showToastMsg(String msg) {
        showToast(msg);
    }

    @Override
    public void dismissProgress() {
        dismissProgressDialog();
    }
}
